import java.sql.Connection;
import java.sql.Statement;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import dao.newDB;

public class SeleniumTest {
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	
	@Before
	public void setUp() throws Exception {
		newDB teskonek = new newDB();
		assertEquals("Koneksi", 1, teskonek.dbOpen());
		driver = new FirefoxDriver();
		baseUrl = "http://localhost:8080/";
		driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
	}
	
	@Test
	public void testSeleniumtestcase() throws Exception {
		driver.get(baseUrl + "/cobapertama/");
		driver.findElement(By.id("name")).clear();
		driver.findElement(By.id("name")).sendKeys("ahmad");
		driver.findElement(By.id("UpdateButton")).click();
		//driver.findElement(By.id("name")).clear();
		//driver.findElement(By.id("name")).sendKeys("syafii");
		//driver.findElement(By.id("UpdateButton")).click();
	}
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
		newDB testkonek = new newDB();
		assertEquals("Hapus", 0, testkonek.deleteContent("ahmad"));
	}
	
	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}